package com.mengjiahui.auduoplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mengjiahui.auduoplayer.bean.Music;
import com.mengjiahui.auduoplayer.R;
import com.mengjiahui.auduoplayer.bean.MusicRank;

import java.io.IOException;
import java.util.List;

/**
 * @author Meng
 * @data 2020/5/17
 */
public class OnlineMusicAdapter extends RecyclerView.Adapter<OnlineMusicAdapter.ViewHolder>{
    private List<MusicRank> musicRankList;
    private OnItemClickListener onItemClickListener;
    private Context context;
    private View view;

    public OnlineMusicAdapter(List<MusicRank> musicRankList){
        this.musicRankList=musicRankList;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context=parent.getContext();
        view= LayoutInflater.from(context).inflate(R.layout.item_online_music,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        try {
            MusicRank musicRank=musicRankList.get(position);
            List<Music> musicList=musicRank.getMusicList();
            Music music1=musicList.get(0);
            Music music2=musicList.get(1);
            Music music3=musicList.get(2);
            holder.musicRankTextView1.setText("1."+music1.getMusicName()+"-"+music1.getAuthor());
            holder.musicRankTextView2.setText("2."+music2.getMusicName()+"-"+music2.getAuthor());
            holder.musicRankTextView3.setText("3."+music3.getMusicName()+"-"+music3.getAuthor());
            holder.itemView.setOnClickListener(v->{
                onItemClickListener.touch(musicRank);
            });
            Glide.with(view.getContext()).load(musicRank.getImg()).into(holder.musicRankImageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return musicRankList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView musicRankImageView;
        private TextView musicRankTextView1;
        private TextView musicRankTextView2;
        private TextView musicRankTextView3;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicRankImageView=itemView.findViewById(R.id.imageViewMusicRank);
            musicRankTextView1=itemView.findViewById(R.id.textViewMusicRank1);
            musicRankTextView2=itemView.findViewById(R.id.textViewMusicRank2);
            musicRankTextView3=itemView.findViewById(R.id.textViewMusicRank3);
        }
    }

    public interface OnItemClickListener{
        void touch(MusicRank musicRank);
    }
    public void setListener(OnlineMusicAdapter.OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }
}
